<template>
  <div class="cate-container">
    <div class="cate-wrap">
      <div
        v-for="(cate, index) in cates"
        :key="cate.id"
        @click="switchCates(index)"
        :class="[
          'cate',
          {
            active: activeIndex === index,
          },
        ]"
      >
        {{ cate.name }}
      </div>
    </div>
    <div class="item-wrap">
      <div class="items">
        <div
          class="item"
          @click="
            router.push({
              path: '/detail',
              query: {
                id: item.id,
              },
            })
          "
          v-for="item in items"
          :key="item.id"
        >
          <img :src="item.pic" alt="" class="item-thumb" />
          <div class="item-info">
            <h5>{{ item.name }}</h5>
            <div class="item-actions">
              <p class="price">￥{{ item.minPrice }}</p>
              <van-icon name="gouwuchetianjia" size="20" class-prefix="icon" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <Tabbar />
</template>

<script setup lang="ts" name="CatePage">
import { ref, onMounted } from "vue";
import Tabbar from "@/components/Tabbar.vue";
import { fetchCates, fetchItems } from "@/api";
import { useRouter } from "vue-router";
const router = useRouter();
// 所有分类
const cates = ref<any[]>([]);
// 高亮分类下对应商品列表
const items = ref<any[]>([]);
// 高亮分类下标
const activeIndex = ref(0);
// 点击切换高亮分类
const switchCates = (index: number) => {
  activeIndex.value = index;
  getItems();
};
// 请求所有分类
const getCates = () => {
  fetchCates().then((res) => {
    if (res.data.code === 0) {
      cates.value = res.data.data;
      getItems();
    }
  });
};
// 请求高亮分类商品列表
const getItems = () => {
  fetchItems({
    categoryId: cates.value[activeIndex.value].id,
  }).then((res) => {
    if (res.data.code === 0) {
      items.value = res.data.data.result;
    }
    if (res.data.code === 700) {
      items.value = [];
    }
  });
};
onMounted(() => {
  getCates();
});
</script>

<style scoped lang="scss">
.cate-container {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 50px;
  display: flex;
  .cate-wrap {
    width: 3rem;
    height: 100%;
    overflow-y: auto;
    border-right: 1px solid #ccc;
    background-color: #fafafa;
    .cate {
      line-height: 40px;
      font-size: 12px;
      color: #333;
      text-align: center;
      &.active {
        color: #e93323;
        background-color: #fff;
        position: relative;
        &::after {
          content: "";
          display: block;
          position: absolute;
          width: 2px;
          height: 40px;
          background-color: #e93323;
          left: 0;
          top: 0;
        }
      }
    }
  }
  .item-wrap {
    width: 7rem;
    height: 100%;
    overflow-y: auto;
    .items {
      .title {
        line-height: 30px;
        font-size: 18px;
        text-align: center;
        background-color: #f1f1f1;
      }
      .item {
        display: flex;
        img {
          width: 2rem;
        }
        .item-info {
          flex: 1;
          font-size: 14px;
          padding: 0 10px;
          h5 {
            line-height: 24px;
          }
          .item-actions {
            display: flex;
            align-items: center;
            justify-content: space-between;
            color: #e93323;
            .price {
              font-size: 20px;
            }
          }
        }
      }
    }
  }
}
</style>
